package com.sonny.jdom;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Content;
import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.filter.ElementFilter;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class ReadXML {

	static String xmlTestFile1 = "toll_report.xml";

	public static void main(String[] args) throws IOException, JDOMException {

		ReadXMLDoc(xmlTestFile1);
		SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(xmlTestFile1);

		// getList1(doc);
		//getList2(doc);
		//getList3(doc);
		getList4(doc); //
		System.out.println("Completed");

	}

	public static void getList1(Document doc) throws IOException, JDOMException {
		Element root = doc.getRootElement();

		trace("Input File : " + xmlTestFile1);
		trace("root.getName() : " + root.getName());
		List children = root.getChildren();
		Iterator iter = children.iterator();
		while (iter.hasNext()) {
			Object obj = iter.next();
			trace(obj.toString());
			trace("[Class Name] " + obj.getClass().getName());
		}
	}

	public static void getList2(Document doc) throws IOException, JDOMException {
		Iterator itr = doc.getDescendants(new ElementFilter("job"));
		while (itr.hasNext()) {
			Content c = (Content) itr.next();
			System.out.println("-----");
			System.out.println(c.getValue());
			System.out.println("-----");
			if (c instanceof Element) {
				System.out.println("Found Element");
				Element e = (Element) c;
				Attribute attr = e.getAttribute("name");
				if (attr != null && attr.getValue().equals("Job 2")) {
					System.out.println("Attribute ID Job 2 is found");
					Iterator itr2 = e.getDescendants(new ElementFilter("method"));
					while (itr2.hasNext()) {
						Element e2 = (Element) itr2.next();
						System.out.println(e2.getValue());
					}
				}
			}
		}
	}

	public static void getList3(Document doc) throws JDOMException {
		{

			Element root = doc.getRootElement();
			Iterator itr = doc.getDescendants(new ElementFilter("job"));
			while (itr.hasNext()) {
				Content c = (Content) itr.next();
				if (c instanceof Element) {
					Element e = (Element) c;
					Attribute attr = e.getAttribute("name");
					System.out.println(attr.getValue());
					
				}
			}
			// if( itr.getName().equals("Id") )
			// System.out.println(element.getText());
			// else if( element.getName().equals("File") ) {
			// // //List items = element.getChildren();
			// for(Element item : items)
			// System.out.println(item.getText());
		}
	}
	
	public static void getList4(Document doc) throws JDOMException {
		{

			Element root = doc.getRootElement();
			Iterator itr = doc.getDescendants(new ElementFilter("job"));

			while (itr.hasNext()) {
				Content c = (Content) itr.next();
				if (c instanceof Element) {
					
					// SINGLE ATTRIBUTES
					Element e = (Element) c;
					Attribute attr = e.getAttribute("name");
					System.out.println(attr.getValue());
					
					// REPEATING ATTRIBUTES
					Iterator itr2 = e.getDescendants(new ElementFilter("attr"));
					while (itr2.hasNext()) {
						Element e2 = (Element) itr2.next();
						Attribute attr2 = e2.getAttribute("name");
						System.out.println(attr2.getName());
						System.out.println(attr2.getValue());
						System.out.println(e2.getTextTrim());
					}
					
				}
			}
		}
	}
	

	public static void ReadXMLDoc(String strXMLFileName) throws IOException {

		try {

			SAXBuilder saxbuilder = new SAXBuilder();

			Document myDocument = saxbuilder.build(new File(strXMLFileName));

			XMLOutputter out = new XMLOutputter(org.jdom.output.Format.getPrettyFormat());
			StringWriter writer = new StringWriter();
			out.output(myDocument, writer);
			System.out.println(writer.toString());

		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		}

	}

	public static void showUsage() {
		System.out.println("Usage : run DOMBuilderExample ");
		System.exit(1);
	}

	public static void trace(String data) {
		System.out.println("[ DOMBuilderExample ] " + data);
	}

}
